package cn.hy.pms.thrift.utils;

import cn.hy.face.common.exception.MessageException;
import cn.hy.pms.thrift.*;
import com.wealoha.thrift.PoolConfig;
import com.wealoha.thrift.ServiceInfo;
import com.wealoha.thrift.ThriftClient;
import com.wealoha.thrift.ThriftClientPool;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;

import java.util.Arrays;
import java.util.List;

/**
 * thrift客户端工具类，使用连接池
 */
public class ThriftUtils {
    public static ThriftClientPool<PmsThriftService.Client> pool;


    public static void init(String tHost, Integer tPort) {
        PoolConfig config = new PoolConfig();
        config.setMinIdle(2);
        config.setMaxTotal(20);
        pool = new ThriftClientPool<>(Arrays.asList(new ServiceInfo(tHost, tPort)), e -> new PmsThriftService.Client(new TBinaryProtocol(e)), config);
    }

    /**
     * 根据用户id集合查询用户信息
     * @param userIds
     * @return
     */
    public static List<SysUserThrift> findUserByUserIdList(List<String> userIds) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<SysUserThrift> sutList = pmsThriftService.findUserByUserIdList(userIds);
            client.finish();
            return sutList;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    public static SysUserThrift findUserByUserId(String userId) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            SysUserThrift sut = pmsThriftService.findUserByUserId(userId);
            client.finish();
            return sut;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 根据用户id查询班级信息
     * @param userId
     * @return
     */
    public static List<SysOfficeThrift> findOfficeByUserIdAndSchoolId(String schoolId, String userId) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<SysOfficeThrift> sut = pmsThriftService.findOfficeByUserIdAndSchoolId(userId, schoolId);
            client.finish();
            return sut;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    public static String findPageUser(int pageNum, int pageSize, String schoolId, String officeId, String groupType, String username, String realName) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            String sut = pmsThriftService.findPageUser(pageNum,pageSize,schoolId,officeId,groupType,username,realName);
            client.finish();
            return sut;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 查询用户信息，使用json字符串参数
     * @param pageNum
     * @param pageSize
     * @param schoolId
     * @param jsonParams
     * @return
     * @throws TException
     */
    public static String findPageUserWithJsonParams(int pageNum, int pageSize, String schoolId, String jsonParams) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            String sut = pmsThriftService.findPageUserWithJsonParams(pageNum,pageSize,schoolId,jsonParams);
            client.finish();
            return sut;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 查询用户Id集合信息，使用json字符串参数
     * @param schoolId
     * @param jsonParams
     * @return
     * @throws TException
     */
    public static String findUserIdListWithJsonParams(  String schoolId, String jsonParams) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            String sut = pmsThriftService.findUserIdListWithJsonParams(schoolId,jsonParams);
            client.finish();
            return sut;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    public static String findUserOffice(String schoolId,List<String> userIdList) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            String sut = pmsThriftService.findUserOfficeBySchoolIdAndUserIdList(schoolId,userIdList);
            client.finish();
            return sut;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }
    private static <T>T handleThriftException(TException e) throws TException {
        if (e instanceof TApplicationException && ((TApplicationException) e).getType() == TApplicationException.MISSING_RESULT) {
            return null;
        } else if (e instanceof ThriftBusinessException) {
            throw new MessageException(((ThriftBusinessException) e).message);
        }  else {
            throw new TException(e);
        }
    }

    /**
     * 根据用户id，应用编码查询用户在那些学校有这些应用
     * @param userId
     * @return
     */
    public static List<SchoolRoleVoThrift> findSchoolByUserIdAndAppCode(String userId, String appCode) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<SchoolRoleVoThrift> srvList = pmsThriftService.findSchoolByUserIdAndAppCode(userId, appCode);
            client.finish();
            return srvList;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 根据学校id, LIKE用户真实姓名 查询用户信息
     * @param
     * @return
     */
    public static List<SysUserThrift> findUserBySchoolIdAndRealNameLike(String schoolId, String realName) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<SysUserThrift> sutList = pmsThriftService.findUserBySchoolIdAndRealNameLike(schoolId, realName);
            client.finish();
            return sutList;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 根据schoolId查询树型机构信息
     * @param
     * @return
     */
    public static List<SysOfficeThrift> findTreeOfficeBySchoolId(String schoolId) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<SysOfficeThrift> sotList = pmsThriftService.findTreeOfficeBySchoolId(schoolId);
            client.finish();
            return sotList;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 根据学校id和其他参数（机构id）(后期动态补充)查询所有用户id信息
     * @param schoolId
     * @param officeId
     * @return
     * @throws TException
     */
    public static List<String> findUserIdBySchoolIdAndOtherParams(String schoolId, String officeId) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<String> userIdList = pmsThriftService.findUserIdBySchoolIdAndOtherParams(schoolId, officeId);
            client.finish();
            return userIdList;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 根据userId、schoolId、appCode查询userId用户在schoolId学校、appCode应用下的权限信息
     * @param userId
     * @return
     */
    public static List<SysMenuPermissionThrift> findMenuPermByUserIdAndSchoolIdAndAppCode(String userId, String schoolId, String appCode) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<SysMenuPermissionThrift> smpList = pmsThriftService.findMenuPermByUserIdAndSchoolIdAndAppCode(userId, schoolId, appCode);
            client.finish();
            return smpList;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }

    /**
     * 根据学校id和其他参数（机构id）(后期动态补充)查询所有用户id信息
     * @param schoolId
     * @param userId
     * @return
     * @throws TException
     */
    public static List<SysRoleThrift> findRoleByUserIdAndSchoolId(String schoolId, String userId) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            List<SysRoleThrift> roleList = pmsThriftService.findRoleByUserIdAndSchoolId(userId, schoolId);
            client.finish();
            return roleList;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }
    
    /**
     * 根据学校id获取年级信息
     * @return
     */
    public static String findGradeBySchoolId(String schoolId) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            String result = pmsThriftService.findGradeBySchoolId(schoolId);
            client.finish();
            return result;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }
    /**
     * 根据学校id,json参数查询班级信息
     * @return
     */
    public static String findOfficeBySchoolIdAndOtherJsonParams(String schoolId, String jsonParams) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            String result = pmsThriftService.findClassBySchoolIdAndJsonParams(schoolId, jsonParams);
            client.finish();
            return result;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }
    /**
     * 根据学校id（必传）,班级id（必传）和其他参数(人员类型，用户id)查询班级用户信息
     * @return
     */
    public static String findClassUserDetailBySchoolIdAndClassIdAndOtherParams(String schoolId, String classId, String type, String userId) throws TException {
        try (ThriftClient<PmsThriftService.Client> client = pool.getClient()) {
            PmsThriftService.Client pmsThriftService = client.iFace();
            String result = pmsThriftService.findClassUserDetailBySchoolIdAndClassIdAndOtherParams(schoolId, classId, type, userId);
            client.finish();
            return result;
        } catch (TException e) {
            return handleThriftException(e);
        }
    }
}
